<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Install extends Gio_Core_Controller
{
	public function step1Action()
	{
		/**
		 * Remove all plugins
		 */
		Gio_Core_Application::getInstance()->removePlugins();
		
		if (Gio_Core_Application::_initInstallChecker()) {
			$this->redirect($this->view->url('core_index_index'));	
		}
		
		$this->setTemplate('admin');
		$this->setLayout('install');
		
		$request = $this->getRequest();
		
		$configs = Gio_Core_Config_Xml::getConfig();
		
		$request = Gio_Core_Request::getInstance();
		if ((string)$configs->web->url != $request->getBaseUrl()) {
			$configs->server->static = $request->getBaseUrl();
			$configs->server->resource = $request->getBaseUrl();
			$configs->web->url = $request->getBaseUrl();
		}
			
		$this->view->configs = $configs;
		
		$timezone = array(
						'timezone' => array(
								'date' 		=> (string)$configs->timezone->date,
								'datetime'  => (string)$configs->timezone->datetime
						)
					);
		$localization = array(
						'localization' => array(
									'enable'	=> (string)$configs->localization->enable,
									'languages'	=> array(
										'default'  => (string)$configs->localization->languages->default,
										'list'	  => (string)$configs->localization->languages->list,
										'details' => array()			
									)	
						)
					);
		if ($configs->localization->languages->list != null) {
			$list = explode(',', $configs->localization->languages->list);
			foreach ($list as $value) {
				$localization['localization']['languages']['details'][$value] = (string)$configs->localization->languages->details->$value;
			}
		}
		
		$languagesFile = ROOT_DIR . DS . 'configs' . DS . 'languages.xml';
		if (!file_exists($languagesFile)) {
			return;
		}
		
		$languagesXml = @simplexml_load_file($languagesFile);
		if (null == $languagesXml) {
			return;
		}
		
		$locales          = array();
		foreach ($languagesXml->language as $language) {
			$arr 			  = explode('|', $language);
			$locales[$arr[0]] = array(
				'code'		  => $arr[0],
				'localName'   => $arr[1],
				'englishName' => $arr[2],
			);
		}
		$this->view->locales = $locales;
		
		if ($request->isPost()) {
			$act = $request->getPost('act');
			switch ($act) {
				case 'testdbconn':
					$this->setNoRender();
					$this->disableLayout();
					$this->_testdbconn();
					break;
				case '':
				default:
					$configs = $request->getPost('configs');
					$data = Modules_Core_Services_Installer::validate($configs);
					
					if (isset($data['messages_error']) && $data['messages_error']) {
						$this->view->errorMessages = $data['messages'];
						$configs = Gio_Core_Array::toObject($configs);
						$this->view->configs = $configs;
						return;
					}
					
					$install = array(
							'install' => array(
											'version' => Gio_Core_Cms::getVersion(),
											'date'	  => date('Y-m-d H:i:s'),
											'author'  => Gio_Core_Cms::getAuthor()
										)
					);
					
					$configs = array_merge($configs, $install);
					$configs = array_merge($configs, $localization);
					$configs = array_merge($configs, $timezone);
					
					$content = utf8_decode(Gio_Core_Array::toXml($configs, 'config'));
					$fileName = Gio_Core_Config_Xml::getConfigFile();
					Gio_Core_File::writeToFile($fileName, $content);
					
					$adminInfo = array(
									'username' => 'admin',
									'password' => '123456',
									'email'	   => 'ninhgio@gmail.com',
									'fullname' => 'Ninhgio',	
								);
					Modules_Core_Services_Installer::install(false, $adminInfo);
					
					$this->redirect($this->view->url('core_index_index'));
					
					break;
			}
		}
	}
	
	/**
	 * Test database connection
	 * 
	 * @return void
	 */
	private function _testdbconn()
	{
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$conn = Gio_Db_Mysql::getInstance(true);
		
		$options = array(
			'host'     => $request->getPost('host'),
			'port' 	   => $request->getPost('port'),
			'username' => $request->getPost('username'),
			'password' => $request->getPost('password'),
		);
		$success = $conn->testConnection($options);
		if ($success) {
			$databases = $conn->getDatabases($options);
			$return    = array('result' => 'RESULT_OK', 'databases' => $databases);
		} else {
			$return    = array('result' => 'RESULT_ERROR', 'databases' => null);
		}

		$json = new Services_JSON();
		$this->getResponse()->setBody($json->encodeUnsafe($return));
	}
}